package com.wk.warehouse.controller;

import com.google.code.kaptcha.Producer;
import com.wk.warehouse.entity.Result;
import com.wk.warehouse.util.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/*
    验证码
*/
@RestController
@RequestMapping("/captcha")
public class VerificationCodeController {

    @Resource(name = "captchaProducer")
    private Producer captchaProducer;
    // Springboot提供的针对redis操作字符串的便捷工具对象
    private static StringRedisTemplate stringRedisTemplate;

    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        VerificationCodeController.stringRedisTemplate = stringRedisTemplate;
    }
    /**
     * 验证码生成接口
     */
    @GetMapping("/captchaImage")
    public void getKaptchaImage(HttpServletResponse response) {
        ServletOutputStream out = null;
        try {
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            response.setHeader("Pragma", "no-cache");
            response.setContentType("image/jpeg");
            String code = captchaProducer.createText();    // 验证码
            BufferedImage bi = captchaProducer.createImage(code); // 图片

            /**
             * 对验证码的key进行了拼接
             * */
            /*StringBuffer sb = new StringBuffer("code:");
            String logo_key = UUID.randomUUID().toString().replaceAll("-", "");//使用uuid生成验证码的key
            sb.append(logo_key);*/
            stringRedisTemplate.opsForValue().set(WarehouseConstants.VERIFICATION_CODE_REDIS_KEY, code); // 将验证码的key和value存入redis中

            out = response.getOutputStream();
            ImageIO.write(bi, "jpg", out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 验证码后台验证
     */
    @GetMapping("/verify-code")
    public Result verifyCode(String verificationCode) {
        String serverCode = stringRedisTemplate.opsForValue().get(WarehouseConstants.VERIFICATION_CODE_REDIS_KEY);
        if(verificationCode.equals(serverCode)){
            return Result.ok("验证码正确");
        }else{
            return Result.err(Result.CODE_ERR_BUSINESS, "验证码不正确！");
        }
    }

}
